import requests
import json

json_file = './category.json'
# /subcategory/detail?miniWallkey=
# baseUrl = 'http://152.136.185.210:7878/api/m5/subcategory/detail?miniWallkey='
baseUrl = 'http://152.136.185.210:7878/api/m5/subcategory/detail?miniWallkey='
w_path = "subcategory/"


home='http://152.136.185.210:7878/api/m5/home/data?'
def request_url():
    with open(json_file, "r", encoding='utf-8') as f:
        t_json = json.load(f)
        ls = t_json['data']['category']['list']
        for k in ls:
            a_url = baseUrl + k['miniWallkey']
            print(a_url)
            txt = requests.get(a_url).json()
            filepath = w_path +  k['miniWallkey'] + ".json"
            with open(filepath, 'w', encoding='utf-8') as f1:
                f1.write(json.dumps(txt, indent=4, ensure_ascii=False))


    # print(json.dumps(t_json, sort_keys=True, indent=2))  # 排序并且缩进两个字符输出

# url = 'http://152.136.185.210:7878/api/m5/subcategory?maitKey=595'
# txt = requests.get(url).json()


# request_url()
# print("ok")

# print(txt['data'])
# with open('00000.json', 'w', encoding='utf-8') as f1:
#    f1.write(json.dumps(txt['data'], indent=4, ensure_ascii=False))


tmp_list = ['pop','new','sell']

for j in range(len(tmp_list)):
    type = tmp_list[j]
    for i in range(10):
        page='type=' + type +'&page='+str(i+1)
        r_url = home + page
        print(r_url)
        txt = requests.get(r_url).json()
        filepath =type + "/" + str(i+1) +".json"

        with open(filepath, 'w', encoding='utf-8') as f1:
            f1.write(json.dumps(txt['data'], indent=4, ensure_ascii=False))


# for i in range(10):
    # print(i+1)
    # ''?type = sell & page = 1

